﻿using AutoMapper;
using ProjectManageSystemApi.DbEntity;
using ProjectManageSystemApi.Enums;
using ProjectManageSystemApi.Models;
using ProjectManageSystemApi.Utilities;
using System;
using System.Collections.Generic;

namespace ProjectManageSystemApi.Services.Impl
{
    public class ProjectItemImageInfoServices : IProjectItemImageInfoServices
    {
        readonly IFreeSql _fsql;
        readonly IMapper _mapper;
        public ProjectItemImageInfoServices(IFreeSql fsql, IMapper mapper)
        {
            _fsql = fsql;
            _mapper = mapper;
        }
        public bool AddOrUpdate(List<ProjectItemImageInfoDto> itemImages, long userId, int itemId)
        {
            try
            {
                _fsql.Transaction(() =>
                    {
                        Delete(itemId);
                        if (itemImages.Count <= 0)
                        {
                            return;
                        }
                        if (!Insert(itemImages, userId, itemId))
                        {
                            throw new DbTranException("增加新项目图片数据失败");
                        }
                    });
                return true;
            }
            catch (Exception)
            {
                return false;
            }

        }
        public bool Insert(List<ProjectItemImageInfoDto> itemImages, long userId, int itemId)
        {
            var datas = _mapper.Map<List<ItemImageInfo>>(itemImages);
            datas.ForEach(x =>
            {
                x.CreateTime = DateTime.Now;
                x.CreateUser = userId;
                x.ItemId = itemId;
            });
            return _fsql.Insert<ItemImageInfo>(datas).ExecuteAffrows() == itemImages.Count;
        }
        public bool Delete(int itemId)
        {
            return _fsql.Delete<ItemImageInfo>().Where(x => x.ItemId == itemId).ExecuteAffrows() > 0;
        }


        public bool InsertMany(AddProjectItemImageInfoDto itemImages, long userId)
        {
            try
            {
                _fsql.Transaction(() =>
                {
                    var datas = new List<ItemImageInfo>();
                    foreach (var imagePath in itemImages.Images) 
                    {
                        if (string.IsNullOrWhiteSpace(imagePath)) 
                        {
                            continue;
                        }
                        var data = new ItemImageInfo
                        {
                            ItemId = itemImages.ItemId,
                            ImageUrl = imagePath,
                            ImageType = itemImages.ImageType,
                            CreateUser = userId,
                            CreateTime = DateTime.Now
                        };
                        datas.Add(data);
                    }
                    var r = _fsql.Insert<ItemImageInfo>(datas).ExecuteAffrows();
                    if(r < itemImages.Images.Count)
                    {
                        throw new DbTranException("增加新项目图片数据失败");
                    }
                });
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public ProjectItemImagesDto GetImagesByItemId(int itemId)
        {
            return new ProjectItemImagesDto
            {
                ActualImages = _mapper.Map<List<ProjectItemImageInfoDto>>(
                    _fsql.Select<ItemImageInfo>().Where(x => x.ItemId == itemId && x.ImageType == (sbyte)ProjectItemImageType.Actual)
                        .OrderByDescending(x => x.CreateTime).ToList()),
                EvidenceImages = _mapper.Map<List<ProjectItemImageInfoDto>>(
                    _fsql.Select<ItemImageInfo>().Where(x => x.ItemId == itemId && x.ImageType == (sbyte)ProjectItemImageType.Evidence)
                        .OrderByDescending(x => x.CreateTime).ToList())
            };
        }

    }
}
